<?php


namespace App\Logic;


use App\Models\Fang;
use App\server\EsServer;
use App\server\RedisServer;

class SecondaryLogic
{
    /**
     * 二手房源信息展示列表
     */
    public static function Secondary()
    {
        $redis=RedisServer::createRedisObject();
        $redis->select(15);
        $size=['data'=>$redis->getAll()];
        return $size;
    }
    /**
     * 二手房源信息列表同步数据库数据
    */
    public static function redisSecondaryList(){
        $redis=RedisServer::createRedisObject();
        $redis->select(15);
        $data= Fang::all()->toArray();
        foreach ($data as $key => $value){
            $key= array_keys($value);
            for ($i=0;$i<count($key);$i++){
                $redis->hset($value['id'],$key[$i],$value[$key[$i]]);
            }
        }
    }
    /**
     * 二手房源信息添加
     */
    public static function SecondaryAdd($request)
    {
        $validated = $request->validate([
            'fang_name' => 'required',
            'fang_xiaoqu' => 'required',
            'fang_addr' => 'required',
            'fang_rent' => 'required',
            'fang_config' => 'required',
            'fang_direction' => 'required',
            'fang_build_area' => 'required',
            'fang_using_area' => 'required',
            'fang_year' => 'required',
            'fang_floor' => 'required',
            'fang_shi' => 'required',
            'fang_ting' => 'required',
            'fang_wei' => 'required',
            'pic' => 'required',
            'fang_status' => 'required',
            'fang_owner' => 'required',
            'fang_desn' => 'required',
        ], [
            'fang_name.required' => '不能为空',
            'fang_xiaoqu.required' => '不能为空',
            'fang_addr.required' => '不能为空',
            'fang_rent.required' => '不能为空',
            'fang_config.required' => '不能为空',
            'fang_direction.required' => '不能为空',
            'fang_build_area.required' => '不能为空',
            'fang_using_area.required' => '不能为空',
            'fang_year.required' => '不能为空',
            'fang_floor.required' => '不能为空',
            'fang_shi.required' => '不能为空',
            'fang_ting.required' => '不能为空',
            'fang_wei.required' => '不能为空',
            'pic.required' => '不能为空',
            'fang_status.required' => '不能为空',
            'fang_owner.required' => '不能为空',
            'fang_desn.required' => '不能为空',
        ]);
        $SecondaryModel = Fang::create($validated);
        return $SecondaryModel;
    }

    /**
     * 二手房源信息删除
     */
    public static function SecondaryDel($fangId)
    {
        if (empty($fangId)) throw new \Exception('未接收到想要删除的房源信息');
        $executeDel = Fang::del('id', $fangId);
        return $executeDel;
    }

    /**
     * 二手房源信息查找
     */
    public static function SecondarySeek($query)
    {
        if (empty($query)) {
            return Fang::list();
        }
        $executeSeek = Fang::Seek('fang_name', $query);
        return $executeSeek;
    }

    /**
     * 二手房源详情
     */
    public static function SecondaryDetails($fangID)
    {
        if (empty($fangID)) throw new \Exception('未接收到想要查看的房源信息');
        $executeDetail = Fang::detail('id', $fangID);
        return $executeDetail;
    }

    /**
     * 二手房源修改
     */
    public static function SecondaryUpdated($SecondaryMessageUpdated)
    {
        unset($SecondaryMessageUpdated['updated_at']);
        unset($SecondaryMessageUpdated['created_at']);
        unset($SecondaryMessageUpdated['deleted_at']);
        $fangID = $SecondaryMessageUpdated['id'];
        $SecondaryModel = Fang::change('id', $fangID, $SecondaryMessageUpdated);
        return $SecondaryModel;
    }

    /**
     * 二手房源es搜索
     */
    public static function SecondarySearch($query)
    {
        if (empty($query)) {
            return Fang::list();
        }
        $executeEsSearch = new EsServer();
        $EsMessage['data'] = $executeEsSearch->searchEs('fang', $query, 'fang_name');
        return $EsMessage;
    }
}
